package com.th.cash.th2.main

import android.R.attr.data
import android.view.View
import androidx.core.content.ContextCompat
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.th.basemodel.base.BaseVDBLazyLoadingFragment2
import com.th.basemodel.base.BaseViewModel
import com.th.basemodel.ext.bindData
import com.th.basemodel.ext.divider
import com.th.basemodel.ext.dp2px
import com.th.basemodel.ext.gotoActivity
import com.th.basemodel.ext.itemClick
import com.th.basemodel.ext.vertical
import com.th.basemodel.utils.AppUtil
import com.th.cash.th2.userliving.UserLivingActivity
import com.th.cash.util.ActivityRouter
import com.th.resmodel.R
import com.th.resmodel.databinding.Fragment3Binding
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

class MineFragment : BaseVDBLazyLoadingFragment2<Fragment3Binding>() {

    private val mMineList = mutableListOf<UserCenterResponse.ZEdJHHaNummuliteQvCZoJC>()

    private val mViewModel by viewModels<MainVm>()

    override fun startObserve() {
        lifecycleScope.launch {
            mViewModel.uiState.collect {
                when (it) {
                    is MainState.Idle -> {

                    }

                    is MainState.UserCenterData ->{
                        //处理UI
                        mMineList.clear()
                        if (it.data.zEdJHHaNummuliteQvCZoJC.isNotEmpty()) {
                            mMineList.addAll(it.data.zEdJHHaNummuliteQvCZoJC)
                        }
                        mBinding.rvMine.adapter?.notifyDataSetChanged()

                        handleTopBtn(mMineList)
                    }


                    else -> {}
                }
            }
        }
    }

    private fun handleTopBtn(list: MutableList<UserCenterResponse.ZEdJHHaNummuliteQvCZoJC>) {
        val historyList = list.filter {
            it.fOEJfxDMilchSvtaQHn == "บันทึกการทําธุรกรรม"
        }

        val gameList = list.filter {
            it.fOEJfxDMilchSvtaQHn == "ศูนย์เกม"
        }

        if (historyList.isEmpty() && gameList.isEmpty()) {
            mBinding.mineBtn.visibility = View.GONE
        }else{
            mBinding.mineBtn.visibility = View.VISIBLE

            mBinding.llTransactionHistory.visibility = if (historyList.isNotEmpty()) {
                mBinding.llTransactionHistory.setOnClickListener {
                    ActivityRouter.next(
                        requireContext(),
                        historyList[0].ODKSaOjEmbowHJdsBQj
                    )
                }

                View.VISIBLE
            }else{
                View.GONE
            }

            mBinding.llGameCenter.visibility = if (gameList.isNotEmpty()) {
                mBinding.llGameCenter.setOnClickListener {
                    ActivityRouter.next(
                        requireContext(),
                        gameList[0].ODKSaOjEmbowHJdsBQj
                    )
                }

                View.VISIBLE
            }else{
                View.GONE
            }
        }
    }

    override fun init() {
        initMineAdapter()
    }

    override fun onLazyInitData() {
        mBinding.mineName.text = AppUtil.userPhone
        mViewModel.intentChannel.trySend(MainIntent.GetUserCenter)
    }

    override fun getVm(): BaseViewModel {
        return mViewModel
    }

    companion object {
        fun newInstance(): MineFragment {
            return MineFragment()
        }
    }

    private fun initMineAdapter() {
        mBinding.rvMine.vertical()
            .divider(color = ContextCompat.getColor(requireContext(), R.color.white),size = dp2px(16f))
            .bindData(mMineList, R.layout.item_mine_btn) { holder, t, position ->

                holder.setText(R.id.mintBtnTitle,t.fOEJfxDMilchSvtaQHn)

                val options = RequestOptions.bitmapTransform(RoundedCorners(6))
                Glide.with(holder.itemView.context)
                    .load(t.TcphyfPTininessJnQSrlx)
                    .apply(options)
                    .into(holder.getView(R.id.mintBtnIcon))
            }.itemClick<UserCenterResponse.ZEdJHHaNummuliteQvCZoJC> { data, _, position ->
                ActivityRouter.next(
                    requireContext(),
                    data[position].ODKSaOjEmbowHJdsBQj
                )
            }
    }

}